#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<string>
#include<unordered_map>
using namespace std;

bool hasArbitrage(int n, vector<vector<double>>& graph)
{
	for (int k = 0; k < n; ++k) 
	{
		for (int i = 0; i < n; ++i) 
		{
			for (int j = 0; j < n; ++j) 
			{
				if (graph[i][k] * graph[k][j] > graph[i][j]) 
				{
					graph[i][j] = graph[i][k] * graph[k][j];
				}
			}
		}
	}
	for (int i = 0; i < n; ++i) 
	{
		if (graph[i][i] > 1.0) 
		{
			cout << "YES";
			return true;
		}
	}
	cout << "NO";
	return false;
}

void test5()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, m;
		cin >> n >> m;
		vector<vector<double>> graph(n, vector<double>(n, 0.0));
		string tmp;
		unordered_map<string, int> um(n);
		for (int i = 0; i < n; i++)
		{
			cin >> tmp;
			um.insert(make_pair(tmp, i));
		}
		double rate;
		string m1, m2;
		for (int i = 0; i < m; i++)
		{
			cin >> m1 >> rate >> m2;
			graph[um[m1]][um[m2]] = rate;
		}
		hasArbitrage(n, graph);
		if (t)
		{
			cout << endl;
		}
	}
}

int main()
{
	test5();
	return 0;
}
